<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>表单输入绑定</h2>
      <!-- 
        onchange: 内容改变并且失去焦点
        oninput: 内容改变
       -->
      <!-- <input type="text" :value="textValue" @input="handleChange" /> -->

      <!-- v-model做数据双向绑定(表单控件的内容和js变量绑定) -->
      <!-- v-model实际上是v-bind以及v-on的语法糖 -->

      <h3>普通文本</h3>
      <input type="text" v-model="textValue" />
      <p>textValue: {{ textValue }}</p>

      <hr />

      <h3>多行文本</h3>
      <textarea v-model="areaValue"></textarea>
      <p>areaValue: {{ areaValue }}</p>

      <hr />

      <h3>复选框</h3>
      <!-- 看这个变量被几个复选框绑定 -->
      <!-- 单个复选框绑定成布尔值 -->
      <input
        type="checkbox"
        v-model="checkValue"
        true-value="yes"
        false-value="no"
      />
      <p>checkValue: {{ checkValue }}</p>

      <!-- 多个复选框的时候绑定成数组 -->
      <input type="checkbox" v-model="likes" value="sing" />唱歌
      <input type="checkbox" v-model="likes" value="dance" />跳舞
      <input type="checkbox" v-model="likes" value="basketball" />篮球
      <p>likes: {{ likes }}</p>

      <hr />

      <h3>单选框</h3>
      <input type="radio" value="男" v-model="sexValue" />男
      <input type="radio" value="女" v-model="sexValue" />女
      <input type="radio" value="未知" v-model="sexValue" />未知
      <p>sexValue: {{sexValue}}</p>

      <hr />

      <h3>下拉框</h3>
      <select v-model="selected">
        <option disabled value="">Please select one</option>
        <option>A</option>
        <option>B</option>
        <option>C</option>
      </select>
      <p>selected: {{ selected }}</p>
    </div>

    <script src="./lib/Vue3.js"></script>
    <script>
      const { createApp } = Vue;

      createApp({
        data() {
          return {
            textValue: "hello123",
            areaValue: "area...",
            checkValue: true,
            likes: ["dance"],
            sexValue: "",
            selected: "",
          };
        },
        methods: {
          handleChange(e) {
            // e.target.value表示的是当前表单控件的内容
            // console.log(e.target.value);
            this.textValue = e.target.value;
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
